﻿using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using Shatkotha.WPF.Contacts.Presenters;
using Shatkotha.WPF.Contacts.Views;
using Shatkotha.WPF.Global;
using Shatkotha.WPF.Global.Modules;
using Shatkotha.WPF.Global.Modules.Contacts;

namespace Shatkotha.WPF.Contacts
{
    public class ContactModule : IContactModule
    {
        private IUnityContainer _container;

        public ContactModule(IRegionManager regionManager, IUnityContainer container)
        {
            Container = container;
            RegionManager = regionManager;

            Container.RegisterType(typeof(IContactView), typeof(ContactView), new ContainerControlledLifetimeManager());
            Container.RegisterType(typeof(IContactPresenter), typeof(ContactPresenter), new ContainerControlledLifetimeManager());

            Container.RegisterType(typeof(IContactListView), typeof(ContactListView), new ContainerControlledLifetimeManager());
            Container.RegisterType(typeof(IContactListPresenter), typeof(ContactListPresenter), new ContainerControlledLifetimeManager());

            Container.RegisterType(typeof(IContactEditorView), typeof(ContactEditorView), new ContainerControlledLifetimeManager());
            Container.RegisterType(typeof(IContactEditorPresenter), typeof(ContactEditorPresenter), new ContainerControlledLifetimeManager());
        }

        public IRegionManager RegionManager { get; set; }

        public IUnityContainer Container
        {
            get { return _container; }
            set { _container = value; }
        }

        public void Initialize()
        {
            RegionManager.Regions[RegionNames.RegionMain].Add(_container.Resolve<IContactPresenter>().View);
            RegionManager.Regions[RegionNames.RegionContactsGrid].Add(_container.Resolve<IContactListPresenter>().View);
        }
    }
}
